<?php
/**
 * SocialEngine
 *
 * @category   Application_Extensions 
 * @package    Seaocore
 * @copyright  Copyright 2009-2010 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: IndexController.php 2010-11-18 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */

class Seaocore_IndexController extends Core_Controller_Action_Standard
{
  public function indexAction()
  {
    $viewer = Engine_Api::_()->user()->getViewer();

    if( !empty($viewer) && !empty($viewer->level_id) ) {
    $level_id = $viewer->level_id;
    if (!$this->_helper->requireUser()->isValid())
      return;
    }
    $product_type=$this->_getParam('type');
    if( ($level_id != 1) || (empty($product_type)) ) {
      return $this->_forward('requireauth', 'error', 'core');
    }
    
    include_once APPLICATION_PATH . '/application/modules/Seaocore/controllers/license/license1.php';
  }

  public function uploadcamimageAction() {
      $session = new Zend_Session_Namespace();

      $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'public/webcam';
    
      if( @is_dir($path) ) {
	// Delete all before inserted files.
	$this->destroy($path);
      }

      // Create directory if not exist.
      if (!@is_dir($path) && !@mkdir($path, 0777, true)) {
	  @mkdir(dirname($path));
	  @chmod(dirname($path), 0777);
	  @touch($path);
	  @chmod($path, 0777);
      }

      $filename = date('YmdHis') . '.jpg';
      $result = file_put_contents('public/webcam/' . $filename, file_get_contents('php://input'));
      if (!$result) {
	  print "ERROR: Failed to write data to $filename, check permissions\n";
	  exit();
      }

      $session->tem_file_name = $filename;
  }

  private function destroy($dir) {
    $handle=opendir($dir);

    while (($file = readdir($handle))!==false) {
      @chmod($dir.'/'.$file, 0777);
      @unlink($dir.'/'.$file);
    }

    closedir($handle);
    return;
  }
  
  public function upgradeSeaoPluginsAction() {
		if ($this->getRequest()->isPost()) {
      Engine_Api::_()->getApi('settings', 'core')->setSetting('is_seaocore_install', 1);
	    $this->view->isPost = TRUE;
		}
  }
}
?>